﻿using System;
using System.IO;
using System.Security.Principal;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTests.Website;
using UnitTests.Website.Controllers.Home;
using Zavand.Web.Mvc.Manana.Framework;
using Route = UnitTests.Website.Controllers.Home.Actions.Index.Route;

namespace UnitTests
{
    [TestClass]
    public class Routing
    {
        private WebApplication _app;

        [TestInitialize]
        public void Init()
        {
            RouteTable.Routes.Clear();
            _app = new WebApplication(true);
            _app.ApplicationStart();
        }
        [TestMethod]
        public void TestMethod1()
        {
            try
            {
                var c = CreateHomeController();
                var route = new Route();

                c.RouteData.Values["Controller"] = route.Controller;
                c.RouteData.Values["Action"] = route.Action;
                c.ControllerContext.RouteData=c.RouteData;

                var result = c.Index(route);
                Assert.IsInstanceOfType(result,typeof(ViewResult));
                //((ViewResult)result).
                //var vdd = new ViewDataDictionary();
                var tdd=new TempDataDictionary();
                var tw=new StringWriter();
                IViewDataContainer vdc = new ViewDataContainer();
                var vr = (ViewResult) result;
                //vr.ExecuteResult(c.ControllerContext);

                var uh = new UrlHelper(c.ControllerContext.RequestContext);
                var url = uh.RouteUrl(route,route);

//            foreach (var ve in vr.ViewEngineCollection)
//            {
//                ViewEngineResult ver=null;
//                try
//                {
//                    ver = ve.FindView(c.ControllerContext, vr.ViewName, vr.MasterName, false);
//                }
//                catch (Exception ex)
//                {
//                    
//                }
//                if (ver!=null)
//               {
//                   vr.View = ver.View;
//                   break;
//               }
//            }
//            var vc = new ViewContext(c.ControllerContext, vr.View, vdc.ViewData, tdd, tw);
//            var hh = new HtmlHelper(vc, vdc);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public HomeController CreateHomeController()
        {
            var c = new HomeController();
            c.ControllerContext = new ControllerContext()
            {
                Controller = c,
                RequestContext = new RequestContext(new MockHttpContext(), new RouteData())
            };
            return c;
        }
        private class MockHttpContext : HttpContextBase
        {
            private readonly IPrincipal _user = new GenericPrincipal(
                     new GenericIdentity("someUser"), null /* roles */);

            public override IPrincipal User
            {
                get
                {
                    return _user;
                }
                set
                {
                    base.User = value;
                }
            }
        }
        class ViewDataContainer:IViewDataContainer
        {
            public ViewDataDictionary ViewData{get; set; }

            public ViewDataContainer()
            {
                ViewData = new ViewDataDictionary();
            }
        }
    }
}
